﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
using Karasoft.Extension;

namespace Karasoft.Web.UI
{
    [DefaultProperty("ShowSeconds")]
    [ToolboxData("<{0}:TimeCombo runat=server></{0}:TimeCombo>")]
    public class TimeCombo : CompositeControl
    {
        DropDownList ddlminutes;
        DropDownList ddlHours;
        DropDownList ddlSeconds;

        [Bindable(true)]
        [Category("Karasoft")]
        [DefaultValue("ثانية")]
        [Localizable(true)]
        public string SecondsText
        {
            get
            {
                String s = (String)ViewState["SecondsText"];
                return ((s == null) ? "ثانية" : s);
            }

            set
            {
                ViewState["SecondsText"] = value;
            }
        }
        [Bindable(true)]
        [Category("Karasoft")]
        [DefaultValue("دقيقة")]
        [Localizable(true)]
        public string MinutesText
        {
            get
            {
                String s = (String)ViewState["MinutesText"];
                return ((s == null) ? "دقيقة" : s);
            }

            set
            {
                ViewState["MinutesText"] = value;
            }
        }
        [Bindable(true)]
        [Category("Karasoft")]
        [DefaultValue("ساعة")]
        [Localizable(true)]
        public string HoursText
        {
            get
            {
                String s = (String)ViewState["HoursText"];
                return ((s == null) ? "ساعة" : s);
            }

            set
            {
                ViewState["HoursText"] = value;
            }
        }
        [Bindable(true)]
        [Category("Karasoft")]
        [DefaultValue("false")]
        public bool ShowSeconds
        {
            get
            {
                if (ViewState["ShowSeconds"] != null)
                {
                    return (bool)ViewState["ShowSeconds"];
                }
                else return false;
            }
            set
            {
                ViewState["ShowSeconds"] = value;
            }
        }

        public DateTime? SelectedTime
        {
            get
            {
                EnsureChildControls();
                return GetDate();
            }
            set
            {
                EnsureChildControls();
                SetSelectedTime(value);
            }
        }

        private DateTime GetDate()
        {
            int secondes = 0;
                if(ShowSeconds)
                    secondes = ddlSeconds.SelectedValue.ToInt();

                return new DateTime(1900, 1, 1, ddlHours.SelectedValue.ToInt(), ddlminutes.SelectedValue.ToInt(), secondes);
            
        }


        protected override void CreateChildControls()
        {
            Controls.Clear();
            CreateSecondsDrobDownList();
            CreateminutesDrobDownList();
            CreateHoursDrobDownList();
            this.Controls.Add(ddlHours);
            this.Controls.Add(ddlminutes);
            this.Controls.Add(ddlSeconds);
            SetSelectedTime(Karasoft.Web.UI.Utility.DateUtility.SADateNow);

        }

        private void SetSelectedTime(DateTime? dateTime)
        {
            ddlSeconds.SelectedValue = dateTime.HasValue == true ? dateTime.Value.Second.ToString() : "0";
            ddlHours.SelectedValue = dateTime.HasValue == true ? dateTime.Value.Hour.ToString() : "0";
            ddlminutes.SelectedValue = dateTime.HasValue == true ? dateTime.Value.Minute.ToString() : "0";
        }

        private void CreateHoursDrobDownList()
        {
           
            ddlHours = new DropDownList();
            for (int i = 0; i < 24; i++)
            {
                ddlHours.Items.Add(new ListItem(string.Format("{0:00}", i.ToArabicString()), i.ToString()));
            }
            
        }

        private void CreateminutesDrobDownList()
        {
            ddlminutes = new DropDownList();
            for (int i = 0; i < 60; i++)
            {
                ddlminutes.Items.Add(new ListItem(string.Format("{0:00}", i.ToArabicString()), i.ToString()));
            }
        }

        private void CreateSecondsDrobDownList()
        {
            ddlSeconds = new DropDownList();
            for (int i = 0; i < 60; i++)
            {
                ddlSeconds.Items.Add(new ListItem(string.Format("{0:00}", i.ToArabicString()), i.ToString()));
            }
        }

       

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write("<table><tr style='text-align:right'>");
            if (ShowSeconds)
                output.Write("<th>" + SecondsText + "</th><th></th>");
            output.Write("<th>" + MinutesText + "</th><th></th><th>" + HoursText + "</th></tr><tr>");
            if (ShowSeconds)
            {
                output.Write("<td>");
                ddlSeconds.RenderControl(output);
                output.Write("</td><td>:</td>");
            }
            output.Write("<td>");
            ddlminutes.RenderControl(output);
            output.Write("</td><td>:</td>");
            output.Write("<td>");
            ddlHours.RenderControl(output);
            output.Write("</td></tr></table>");
        }
    }
}
